package com.atguigu.lease.web.admin.controller.login;


import com.atguigu.lease.common.context.LoginUserContex;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.UUID;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private LoginService loginService;



    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(150, 80,4);

        String base64 = specCaptcha.toBase64();
        System.out.println(base64);
        String lowerCase = specCaptcha.text().toLowerCase();
        System.out.println(lowerCase);
        String key = UUID.randomUUID().toString().replace("-", "");

        loginService.saveCaptcha(key, lowerCase);


        CaptchaVo captchaVo = new CaptchaVo(base64, key);
        return Result.ok(captchaVo);
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {


        return Result.ok(loginService.login(loginVo));
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info(HttpServletRequest request) {
        System.out.println("===================");
        System.out.println(LoginUserContex.getUser());
        Map<String, String> userMap = LoginUserContex.getUser();
        String id = userMap.get("userId");




        return Result.ok(loginService.getUserInfoById(id));
    }
}